'''
管理员视图
'''
from interface import admin_interface
from lib import common
from interface import common_interface

user_info = {
    'user': None
}


def register():
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()
        re_password = input('请再次输入密码：').strip()
        if not password == re_password:
            print('用户名或者密码输入错误，请重新输入！')
            continue
        flag, msg = admin_interface.register_interface(
            username, password
        )
        print(msg)
        if flag:
            break


def login():
    while True:
        username = input('请输入用户名：').strip()
        password = input('请输入密码：').strip()

        flag, msg = common_interface.login_interface(
            username, password,user_type='admin'
        )
        print(msg)
        if flag:
            # 可变类型不用global
            user_info['user'] = username
            break


@common.auth('admin')
def create_school():
    while True:
        school_name = input("请输入学校名称：").strip()
        school_address = input("请输入学校地址：").strip()

        flag, msg = admin_interface.create_school_interface(
            school_name, school_address, user_info.get('user')
        )
        print(msg)
        if flag:
            break


@common.auth('admin')
def create_course():
    while True:
        flag, school_list = common_interface.get_all_school_interface()
        if not flag:
            print("学校不存在")
            break

        for index, school_name in enumerate(school_list):
            print(index, school_name)

        choice = input("请输入学校编号：").strip()

        if not choice.isdigit():
            print('请输入数字！')
            continue

        choice = int(choice)
        if choice not in range(len(school_list)):
            print("请输入正确编号！")
            continue

        school_name = school_list[choice]
        course_name = input('请输入要创建的课程名称：').strip()

        flag, msg = admin_interface.create_course_interface(
            school_name, course_name, user_info.get('user')
        )
        print(msg)
        if flag:
            break


@common.auth('admin')
def create_teacher():
    while True:
        teacher_name = input('请输入要教师姓名：').strip()

        flag, msg = admin_interface.create_teacher_interface(
            teacher_name, user_info.get('user')
        )

        print(msg)
        if flag:
            break


admin_dic = {
    '1': register,
    '2': login,
    '3': create_school,
    '4': create_course,
    '5': create_teacher
}


def view():
    while True:
        print('''
        ====== 欢迎来到选课系统 ======
                1.注册
                2.登录
                3.创建学校
                4.创建课程
                5.创建讲师
        =========== end ===========
        ''')

        choice = input('请输入功能编号：').strip()
        if choice not in admin_dic:
            print("请输入正确的编号！")
            continue

        admin_dic.get(choice)()
